<template>
  <view class="uni-container">
    <uni-forms ref="form" :model="formData" validateTrigger="bind">
      <uni-forms-item name="merchant_id" label="申请提现商户ID">
        <uni-easyinput placeholder="申请提现商户ID" v-model="formData.merchant_id"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="applyuser_id" label="">
        <uni-easyinput placeholder="申请人ID" v-model="formData.applyuser_id"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="amount" label="提现金额" required>
        <undefined v-model="formData.amount"></undefined>
      </uni-forms-item>
      <uni-forms-item name="fee" label="手续费">
        <undefined v-model="formData.fee"></undefined>
      </uni-forms-item>
      <uni-forms-item name="actual_amount" label="实际到账金额">
        <undefined v-model="formData.actual_amount"></undefined>
      </uni-forms-item>
      <uni-forms-item name="withdraw_type" label="提现方式" required>
        <uni-data-checkbox v-model="formData.withdraw_type" :localdata="formOptions.withdraw_type_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="available_balance" label="可用余额">
        <undefined v-model="formData.available_balance"></undefined>
      </uni-forms-item>
      <uni-forms-item name="frozen_balance" label="冻结余额">
        <undefined v-model="formData.frozen_balance"></undefined>
      </uni-forms-item>
      <uni-forms-item name="remark" label="备注">
        <uni-easyinput placeholder="备注" v-model="formData.remark"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="reason" label="拒绝原因">
        <uni-easyinput placeholder="拒绝原因(当status=2时)" v-model="formData.reason"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="status" label="状态" required>
        <uni-data-checkbox v-model="formData.status" :localdata="formOptions.status_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="account_info" label="支付宝账户信息" required>
        <undefined v-model="formData.account_info"></undefined>
      </uni-forms-item>
      <uni-forms-item name="audit_info" label="审核信息">
        <undefined v-model="formData.audit_info"></undefined>
      </uni-forms-item>
      <uni-forms-item name="payment_info" label="打款信息">
        <undefined v-model="formData.payment_info"></undefined>
      </uni-forms-item>
      <uni-forms-item name="create_date" label="申请时间">
        <uni-datetime-picker return-type="timestamp" v-model="formData.create_date"></uni-datetime-picker>
      </uni-forms-item>
      <view class="uni-button-group">
        <button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
        <navigator open-type="navigateBack" style="margin-left: 15px;">
          <button class="uni-button" style="width: 100px;">返回</button>
        </navigator>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/intangible_withdrawalapply.js';

  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'intangible_withdrawalapply';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.includes(key)) {
        result[key] = validator[key]
      }
    }
    return result
  }

  

  export default {
    data() {
      let formData = {
        "merchant_id": "",
        "applyuser_id": "",
        "amount": null,
        "fee": 0,
        "actual_amount": null,
        "withdraw_type": 0,
        "available_balance": null,
        "frozen_balance": null,
        "remark": "",
        "reason": "",
        "status": 0,
        "account_info": null,
        "audit_info": null,
        "payment_info": null,
        "create_date": null
      }
      return {
        formData,
        formOptions: {
          "withdraw_type_localdata": [
            {
              "text": "支付宝",
              "value": 0
            }
          ],
          "status_localdata": [
            {
              "text": "审核中",
              "value": 0
            },
            {
              "text": "已通过",
              "value": 1
            },
            {
              "text": "已拒绝",
              "value": 2
            },
            {
              "text": "处理中",
              "value": 3
            },
            {
              "text": "已打款",
              "value": 4
            },
            {
              "text": "打款失败",
              "value": 5
            }
          ]
        },
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onLoad(e) {
      if (e.id) {
        const id = e.id
        this.formDataId = id
        this.getDetail(id)
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      
      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
          uni.showToast({
            title: '修改成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      },

      /**
       * 获取表单数据
       * @param {Object} id
       */
      getDetail(id) {
        uni.showLoading({
          mask: true
        })
        db.collection(dbCollectionName).doc(id).field("merchant_id,applyuser_id,amount,fee,actual_amount,withdraw_type,available_balance,frozen_balance,remark,reason,status,account_info,audit_info,payment_info,create_date").get().then((res) => {
          const data = res.result.data[0]
          if (data) {
            this.formData = data
            
          }
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        }).finally(() => {
          uni.hideLoading()
        })
      }
    }
  }
</script>
